package com.itheima.controller;

import com.alibaba.fastjson.JSONObject;
import com.itheima.entity.Result;
import com.itheima.pojo.Type;
import com.itheima.service.TypeService;
import com.itheima.service.impl.TypeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author ：spt
 * @date ：Created in 2020/12/14
 * @description ：商品类型控制器
 * @version: 1.0
 */
@WebServlet("/type")
public class TypeController extends BaseServlet {

        private TypeService typeService = new TypeServiceImpl();

        /**
         * 查询所有商品类型数据
         * 调用Service获取列表
         * 把列表数据封装到Result对象
         * 响应JSON数据
         */
        public void findAll(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
                //调用servcie查询
                List<Type> list = typeService.findAll();
                Result result = null;
                if(list != null){
                    //封装返回结果
                        result = new Result(true, "操作成功", list);
                }else{
                        result = new Result(false, "获取失败");
                }
                response.getWriter().print(JSONObject.toJSON(result));
        }
}
